package com.school.gift.controller;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.Skim;
import com.school.gift.domain.User;
import com.school.gift.model.GoodsModel;
import com.school.gift.service.GoodsService;
import com.school.gift.service.SkimService;
import com.school.gift.service.UserService;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.ip.IPUtil;
import zhibi.common.util.security.SecurityUtil;

import java.util.Date;

/**
 * Created by 执笔 on 2017/3/4.
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SkimService skimService;

    @RequestMapping({"/", "index"})
    public String index(Model model) {
        Example example = ExampleUtil.getInstance()
                .addOrder("sort", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<GoodsModel> pageInfo = goodsService.selectModel(example,new Page(20));
        model.addAttribute("list",pageInfo.getList());
        return "/index";
    }

    /**
     * @return
     */
    @RequestMapping(value = "join", method = RequestMethod.GET)
    public String login() {
        if (null != sessionUser()) {
            throw new MessageException("请先退出当前登录用户");
        }
        return "/join";
    }


    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String phone, String password) {
        User user = userService.selectByPhone(phone);
        if (null == user) {
            throw new MessageException("该用户不存在");
        }
        if (!user.getPassword().equalsIgnoreCase(SecurityUtil.MD5(password, PASSWORD_COUNT))) {
            throw new MessageException("用户名或密码错误");
        }
        if(user.getStatus() == STATE_0){
            throw new MessageException("用户已经被锁定");
        }
        session.setAttribute(SESSION_USER, user);
        Skim skim = new Skim();
        skim.setSession(session.getId());
        Skim temp = skimService.selectOne(skim);
        if(null == temp){
            skim.setIp(IPUtil.getRemortIP(request));
            skim.setAddtime(new Date());
            skim.setUsername(user.getUsername());
            skimService.insertSelective(skim);
        }else{
            temp.setIp(IPUtil.getRemortIP(request));
            temp.setUsername(user.getUsername());
            skimService.updateByIdSelective(temp);
        }

        return redirect("index");
    }


    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String register(User user){
    	User temp = userService.selectByPhone(user.getPhone());
        if(null != temp){
            throw new MessageException("该手机号已经注册");
        }
        user.setAddtime(new Date());
        user.setStatus(1);
        user.setType(0);
        user.setPassword(SecurityUtil.MD5(user.getPassword(),PASSWORD_COUNT));
        userService.insertSelective(user);
        session.setAttribute(SESSION_USER, user);
        return redirect("index");
    }

    @RequestMapping("logout")
    public String logout(){
        session.removeAttribute(SESSION_USER);
        return redirect("index");
    }

}
